<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller as BaseController;
use App\Repository\Sms\SmsTmpRepository;
use Illuminate\Http\Request;

// 分销商
class MessageTmpController extends BaseController
{
    public $repoist;
    public function __construct(SmsTmpRepository $model)
    {
        $this->repoist = $model;
    }

    public function store(Request $request)
    {
        return $this->repoist->add($request);
    }

    public function index(Request $request)
    {
        $where = [];
        if (!empty($request->get('number'))) {
            $where['like'] = ['name' => $request->get('number')];
        }
        if (!empty($request->get('use_type'))) {
            $where['where'] = ['use_type' => $request->get('use_type')];
        }
        if (!empty($request->get('verify_status'))) {
            $where['where'] = ['verify_status' => $request->get('verify_status')];
        }
        if (!empty($request->get('pageNum'))) {
            $where['limit']['start'] = ($request->get('pageNum') - 1) * $request->get('pageSize');
            $where['limit']['number'] = $request->get('pageSize');
            $where['select'] = ['tpl_id', 'use_type', 'name', 'verify_status', 'type', 'content', 'shop_id', 'id', 'created_at', 'updated_at'];

        } else {
//            作为选项
            $where['select'] = ['id', 'name'];
        }

        if ($request->user->role_id != 1) {
            $where['where']['shop_id'] = $request->user->shop_id;
        }
        $data = $this->repoist->index($where);
        return $data;
    }

//同步更新短信模版审核状态
    public function refresh(Request $request)
    {
        if (empty($request->get('tplId'))) {
            return $this->returnMsg(1, '短信模版id参数异常');
        }
        $tplId = $request->get('tplId');
        $tplType = $request->get('type');
        return $this->repoist->refresh($tplId, $tplType);
    }

    public function save(Request $request)
    {
        if (!$request->get('id')) {
            return $this->returnMsg(1, '请传递id参数');
        }
        $where = ['id' => $request->get('id'), 'shop_id' => $request->user->shop_id];
        return $this->repoist->edit($where, $request);
    }

    public function show(Request $request, $id)
    {
        $where = ['id' => $id, 'shop_id' => $request->user->shop_id];
        //查询一条数据
        $return = $this->repoist->getOne($where, ['id', 'name', 'content']);

        return $this->returnMsg(200, '查询成功', $return);
    }

    public function listss(Request $request)
    {
        $where = [];
        if (!empty($request->get('name'))) {
            $where['where'] = ['name' => $request->get('name')];
        }
        $where['where']['shop_id'] = $request->user->shop_id;
        // var_dump($where);
        $return = $this->repoist->lists($where, 1, 99999);
        return $return;
    }

    public function destroy(Request $request, $id)
    {
        if (!$id) {
            return $this->returnMsg(1, '请传递参数');
        }
        $where = [
            'id' => $id,
            'shop_id' => $request->user->shop_id
        ];
        return $this->repoist->del($where);
    }

    public function updateStatus(Request $request, $id)
    {
        $where = [
            'id' => $id,
            'shop_id' => $request->user->shop_id
        ];
        $update = [
            'status' => $request->get('status'),
        ];
        return $this->repoist->edit($where, $request);
    }
}
